package com;

import inputHelp.Utf7ImeHelper;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.R.integer;
import android.os.RemoteException;


import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.db.TxtHelper2;

public class CopyJi2 extends UiAutomatorTestCase {
	
	
	//复读主方法， 主要是获取元素，再获取语句，进行转换后发送
	//copyType： 1图灵；2直接复读
	TxtHelper2 TxtHelper2 = new TxtHelper2();
	DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static int FrameLayout = 0;
	static int RelativeLayout = 0;
	static int AbsListView = 1;
	/**
	 * @param chat   qq的哪一个会话
	 * @param timeLoop  复读一次后等待时间
	 * @param serverIP  图灵服务器地址 一般是本地
	 * @param copyType  1图灵；2直接复读
	 * @param isImg  0不复读图片；1复读图片
	 * @throws RemoteException
	 * @throws UiObjectNotFoundException
	 * @throws IOException
	 */
	

	
	public void fudu(String chat1,String chat2,  String serverIP,int copyType,int isQQ ,int isCopy2,int isLight) throws RemoteException,
			UiObjectNotFoundException, IOException {
		String text = "";
		String userName = "";
		String sayString = "";
		String tmpString = "";
		String tmpnoString = "";
		String notextString = "!!!no textview";
		int count = 0;
        System.out.println(sdf.format(new Date())); 
        
		while(true){
			//sleep(3000);
			UiObject uio;
			UiObject uioName = null;
			UiObject uiot = null;
			
			
				if(null==getDescUiO()||!getDescUiO().exists()){
					System.out.println("!!!!: uio not exits");
					continue;
				}
				uio = getDescUiO();
				
				//com.tencent.mobileqq:id/chat_item_content_layout
				//直接获取textview  说的话
				if(uio.getChild(new UiSelector().resourceId("com.tencent.mobileqq:id/chat_item_content_layout")).exists()){
					uiot = uio.getChild(new UiSelector().resourceId("com.tencent.mobileqq:id/chat_item_content_layout"));
				}else {
					if(!notextString.equals(tmpnoString)){
						System.out.println(sdf.format(new Date())+notextString);
						tmpnoString=notextString;
					}
					continue;
				}
				
				
				if(uiot.getClassName().equals("android.widget.TextView")){
					
				}else if (uiot.getClassName().equals("android.widget.LinearLayout")) {
					uiot = uio.getChild(new UiSelector().className(
							"android.widget.TextView"));
				}else { 
					continue;
				}
				
				if(null==uiot||!uiot.exists()){
					continue;
				}
				sayString = uiot.getText();
			
			//获取发言人
			if(getDescUiO().getChild(new UiSelector().resourceId("com.tencent.mobileqq:id/name")).exists()){
					UiObject uioabs = getDescUiO().getChild(new UiSelector().resourceId("com.tencent.mobileqq:id/name"));
					if(uioabs.getChild(new UiSelector().className(
							"android.widget.TextView")).exists()){
						uioName = uioabs.getChild(new UiSelector().className(
								"android.widget.TextView"));
					}
			}
			
			
			if (uioName!=null) {
				userName = uioName.getText();
			}
			text = userName+":"+sayString;
			
			
			if(text.contains("。；")){
				continue;
			}
			text=text.replace("@", "");
			if(!text.equals(tmpString)){
				System.out.println("original："+text);
				tmpString=text;
				tmpnoString="";
			}
			
			/*String[] sourceStrArray = text.split("::");
			System.out.println(sourceStrArray.length+"split:"+sourceStrArray[0]+","+sourceStrArray[1]);*/
			
			
			//如果isSentExist已经复读过，就不复读了
			//如果isSentExist没复读，先看是不是有人复读过了，没复读过就不复读
			if(isCopy2==1){
				if(TxtHelper2.isSentExist(text)){
					System.out.println(sdf.format(new Date())+ "!!text exist!!"+text);
					continue;
				}else{
					if(!TxtHelper2.isExist(text)){
						TxtHelper2.insertMsg(text);
						System.out.println(sdf.format(new Date())+" isCopy2 done insert:"+text);
						continue;
					}else if(TxtHelper2.isUserDif(text)){
						sendText(getCopyString(text));
						TxtHelper2.insertSentMsg(text);
						System.out.println(sdf.format(new Date())+" isCopy2 done sentInsert:"+text);
					}else {
						continue;
					}
				}
			}else{
				if(TxtHelper2.isSentExist(text)){
					System.out.println(sdf.format(new Date())+"!!text exist!! "+text);
					continue;
				}else{
					sendText(getCopyString(text));
					TxtHelper2.insertSentMsg(text);
					System.out.println(sdf.format(new Date())+"done sentInsert:"+text);
				}
			}
		
			//text = strRandom(text);
			System.out.println("$$$copy Times:  " + count);
			count++;
			//sleep(10*1000+Math.round(Math.random()*5*1000));
		}
		
	}
	
	public static String getCopyString(String str){
		String[] sourceStrArray = str.split("::");
		return sourceStrArray[1];
	}
	
	

	
	public static UiObject getDescUiO3()
			throws UiObjectNotFoundException {
		//System.out.print("geting Frame1");
		// 一层层获取
		UiObject uio1 = new UiObject(new UiSelector().className(
				"android.widget.FrameLayout").index(0));
		if(uio1.exists()){
			//System.out.print("geting relative0");
			UiObject uio2 = uio1.getChild(new UiSelector().className(
					"android.widget.RelativeLayout").index(0));
			if(uio2.exists()){
				//System.out.print("geting abslist2");
				UiObject uio3 = uio2.getChild(new UiSelector().className(
						"android.view.View"));
				if(uio3.exists()){
					UiObject uio4 ;
					//System.out.print("geting relativeN");
					uio4 = getDesc(uio3);
					return uio4;
				}else return null;
			}else return null;
		}else return null;
	}
	
	public static UiObject getDescUiO()
			throws UiObjectNotFoundException {
		//System.out.print("geting Frame1");
		// 一层层获取
		UiObject uio1 = new UiObject(new UiSelector().className(
				"android.widget.FrameLayout").index(FrameLayout));
		if(uio1.exists()){
			//System.out.print("geting relative0");
			UiObject uio2 = uio1.getChild(new UiSelector().className(
					"android.widget.RelativeLayout").index(RelativeLayout));
			if(uio2.exists()){
				//System.out.print("geting abslist2");
				UiObject uio3 = uio2.getChild(new UiSelector().className(
						"android.widget.AbsListView"));
				if(uio3.exists()){
					UiObject uio4 ;
					//System.out.print("geting relativeN");
					uio4 = getDesc(uio3);
					return uio4;
				}else return null;
			}else return null;
		}else return null;
	}
	
	
	
	
	
	public static UiObject getDesc(UiObject uio) throws UiObjectNotFoundException{
		UiObject uioDesc = null;
		for(int i =6;i>=0;i--){
			if(uio.getChild(new UiSelector().className(
					"android.widget.RelativeLayout").index(i)).exists()){
				
				uioDesc = uio.getChild(new UiSelector().className(
						"android.widget.RelativeLayout").index(i));
				break;
			}//else System.out.println(uio.getChildCount()+"uio.getChildCount()");
		}
		return uioDesc;
	}

	public static void sendText(String desct) throws UiObjectNotFoundException {
		UiObject textform = new UiObject(
				new UiSelector().className("android.widget.EditText"));
		textform.click();
		// System.out.println("###"+desct);
		textform.setText(Utf7ImeHelper.e(desct));
		new UiObject(new UiSelector().text("发送")).click();
		// sleep(50000);
	}
	
}
